package com.starsea.book.ui.newpage.normalview.delegate

import android.content.Context
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import android.util.AttributeSet
import android.widget.CompoundButton
import android.widget.TextView
import com.starsea.book.R
import com.starsea.book.ui.newpage.utils.DrawableUtil

/**
 * Function: 设置CompoundButton ButtonDrawable相关代理
 * Description:
 */
open class RadiusCompoundButtonDelegate(view: TextView?, context: Context?, attrs: AttributeSet?) :
  RadiusTextViewDelegate(view, context, attrs) {
  private var mButton: CompoundButton? = null
  private var mStateButtonDrawable: StateListDrawable? = null
  private var mButtonDrawableColorRadius = 0f
  private var mButtonDrawableColorCircleEnable = false
  private var mButtonDrawableWidth = 0
  private var mButtonDrawableHeight = 0
  private var mButtonDrawable: Drawable? = null
  private var mButtonPressedDrawable: Drawable? = null
  private var mButtonDisabledDrawable: Drawable? = null
  private var mButtonSelectedDrawable: Drawable? = null
  private var mButtonCheckedDrawable: Drawable? = null
  override fun initAttributes(context: Context?, attrs: AttributeSet?) {
    mButtonDrawableColorRadius =
      mTypedArray.getDimension(R.styleable.RadiusSwitch_rv_buttonDrawableColorRadius, 0f)
    mButtonDrawableColorCircleEnable = mTypedArray.getBoolean(
      R.styleable.RadiusSwitch_rv_buttonDrawableColorCircleEnable,
      false
    )
    mButtonDrawableWidth =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_buttonDrawableWidth, -1)
    mButtonDrawableHeight =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_buttonDrawableHeight, -1)
    mButtonDrawable = mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_buttonDrawable)
    mButtonPressedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_buttonPressedDrawable)
    mButtonDisabledDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_buttonDisabledDrawable)
    mButtonSelectedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_buttonSelectedDrawable)
    mButtonCheckedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_buttonCheckedDrawable)
    mButtonPressedDrawable =
      if (mButtonPressedDrawable == null) mButtonDrawable else mButtonPressedDrawable
    mButtonDisabledDrawable =
      if (mButtonDisabledDrawable == null) mButtonDrawable else mButtonDisabledDrawable
    mButtonSelectedDrawable =
      if (mButtonSelectedDrawable == null) mButtonDrawable else mButtonSelectedDrawable
    mButtonCheckedDrawable =
      if (mButtonCheckedDrawable == null) mButtonDrawable else mButtonCheckedDrawable
    super.initAttributes(context, attrs)
  }

  override fun init() {
    setButtonDrawable()
    super.init()
  }

  /**
   * 设置drawable宽度--ColorDrawable有效其它不知为啥失效
   *
   * @param drawableWidth
   * @return
   */
  fun setButtonDrawableWidth(drawableWidth: Int): RadiusCompoundButtonDelegate {
    mButtonDrawableWidth = drawableWidth
    return this
  }

  /**
   * 设置drawable高度--ColorDrawable有效其它不知为啥失效
   *
   * @param drawableHeight
   * @return
   */
  fun setButtonDrawableHeight(drawableHeight: Int): RadiusCompoundButtonDelegate {
    mButtonDrawableHeight = drawableHeight
    return this
  }

  /**
   * 设置默认状态Drawable
   *
   * @param drawable
   */
  fun setButtonDrawable(drawable: Drawable?): RadiusCompoundButtonDelegate {
    mButtonDrawable = drawable
    return this
  }

  fun setButtonDrawable(resId: Int): RadiusCompoundButtonDelegate {
    return setButtonDrawable(getDrawable(resId))
  }

  /**
   * 设置按下效果Drawable
   *
   * @param drawable
   */
  fun setButtonPressedDrawable(drawable: Drawable?): RadiusCompoundButtonDelegate {
    mButtonPressedDrawable = drawable
    return this
  }

  fun setButtonPressedDrawable(resId: Int): RadiusCompoundButtonDelegate {
    return setButtonPressedDrawable(getDrawable(resId))
  }

  /**
   * 设置不可操作效果Drawable
   *
   * @param drawable
   */
  fun setButtonDisabledDrawable(drawable: Drawable?): RadiusCompoundButtonDelegate {
    mButtonDisabledDrawable = drawable
    return this
  }

  fun setButtonDisabledDrawable(resId: Int): RadiusCompoundButtonDelegate {
    return setButtonDisabledDrawable(getDrawable(resId))
  }

  /**
   * 设置选中效果Drawable
   *
   * @param drawable
   * @return
   */
  fun setButtonSelectedDrawable(drawable: Drawable?): RadiusCompoundButtonDelegate {
    mButtonSelectedDrawable = drawable
    return this
  }

  fun setButtonSelectedDrawable(resId: Int): RadiusCompoundButtonDelegate {
    return setButtonSelectedDrawable(getDrawable(resId))
  }

  /**
   * 设置Checked状态Drawable
   *
   * @param drawable
   * @return
   */
  fun setButtonCheckedDrawable(drawable: Drawable?): RadiusCompoundButtonDelegate {
    mButtonCheckedDrawable = drawable
    return this
  }

  fun setButtonCheckedDrawable(resId: Int): RadiusCompoundButtonDelegate {
    return setButtonCheckedDrawable(getDrawable(resId))
  }

  /**
   * 设置CompoundButton的setButtonDrawable属性
   */
  private fun setButtonDrawable() {
    mButton = mView as CompoundButton?
    if (mButtonDrawable != null || mButtonPressedDrawable != null || mButtonDisabledDrawable != null || mButtonSelectedDrawable != null || mButtonCheckedDrawable != null) {
      val radius =
        if (mButtonDrawableColorCircleEnable) (mButtonDrawableWidth + mButtonDrawableHeight / 2).toFloat() else mButtonDrawableColorRadius
      if (mStateButtonDrawable == null) {
        mStateButtonDrawable = StateListDrawable()
      }
      mStateButtonDrawable?.addState(
        intArrayOf(mStateChecked),
        getStateDrawable(
          mButtonCheckedDrawable,
          radius,
          mButtonDrawableWidth,
          mButtonDrawableHeight
        )
      )
      mStateButtonDrawable?.addState(
        intArrayOf(mStateSelected),
        getStateDrawable(
          mButtonSelectedDrawable,
          radius,
          mButtonDrawableWidth,
          mButtonDrawableHeight
        )
      )
      mStateButtonDrawable?.addState(
        intArrayOf(mStatePressed),
        getStateDrawable(
          mButtonPressedDrawable,
          radius,
          mButtonDrawableWidth,
          mButtonDrawableHeight
        )
      )
      mStateButtonDrawable?.addState(
        intArrayOf(mStateDisabled),
        getStateDrawable(
          mButtonDisabledDrawable,
          radius,
          mButtonDrawableWidth,
          mButtonDrawableHeight
        )
      )
      mStateButtonDrawable?.addState(
        intArrayOf(),
        getStateDrawable(
          mButtonDrawable,
          radius,
          mButtonDrawableWidth,
          mButtonDrawableHeight
        )
      )
      DrawableUtil.setDrawableWidthHeight(
        mStateButtonDrawable,
        mButtonDrawableWidth,
        mButtonDrawableHeight
      )
      mButton!!.buttonDrawable = mStateButtonDrawable
    }
  }
}